package com.fone.player.activity.local;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import com.fone.player.R;
import com.fone.player.activity.local.adapter.LocalFolderAdapter;
import com.fone.player.activity.local.adapter.LocalMediaAdatper;
import com.fone.player.entity.MediaFile;
import com.fone.player.entity.MediaFolder;
import com.fone.player.play.FonePlayerActivity;
import com.fone.player.play.PlayerFrom;
import com.fone.player.util.L;

public class LocalFolderFragment extends Fragment {

	private String TAG = "LocalFolderFragment";
	private View mView;
	private ListView lv_local_folder;
	private ListView lv_local_folder_file;
	private List<MediaFile> mFolderFiles;
	private List<MediaFolder> mFolders;
	private LocalMediaAdatper mFolderFileAdapter;
	private LocalFolderAdapter mFolderAdapter;
	private List<Boolean> mCheckBoxs;
	private View folder_file_view;
	private IFolderClickListener mFolderClickListener;
	private Button backButton;
	private Handler mHandler;
	public void setViewPager(ViewPager mViewPager) {
	}

	public final static int FOLDER_MODE = 0, FOLDER_FILE_MODE = 1;
	public  int MODE_TYPE = FOLDER_MODE;

	public void setHandler(Handler mHandler) {

		this.mHandler = mHandler;
	}

	public LocalFolderFragment() {

	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mView = LayoutInflater.from(getActivity()).inflate(
				R.layout.personal_local_file, null);
		ViewGroup parent = (ViewGroup) mView.getParent();
		if (parent != null) {
			parent.removeView(mView);
		}
		initView();
		return mView;
	}

	private void initView() {
		mCheckBoxs = new ArrayList<Boolean>();
		mCheckBoxs.add(false);
		lv_local_folder = (ListView) mView.findViewById(R.id.lv_local_file);
		folder_file_view = mView.findViewById(R.id.folder_file_view);
		folder_file_view.setVisibility(View.GONE);
		lv_local_folder_file = (ListView) mView
				.findViewById(R.id.lv_local_folder_file);
		Button rightButton = (Button) mView
				.findViewById(R.id.common_title_right_bt);
		rightButton.setVisibility(View.GONE);
		backButton = (Button) mView.findViewById(R.id.common_title_left_bt);
		backButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				changeModeType(FOLDER_MODE);
			}

		});
		mFolderFileAdapter = new LocalMediaAdatper(getActivity(), mFolderFiles,
				mCheckBoxs, lv_local_folder_file, mHandler);

		lv_local_folder_file = (ListView) mView
				.findViewById(R.id.lv_local_folder_file);
		lv_local_folder_file.setAdapter(mFolderFileAdapter);
		VideosItemClickListener FilesItemClickListener = new VideosItemClickListener();
		lv_local_folder_file.setOnItemClickListener(FilesItemClickListener);
		mFolderAdapter = new LocalFolderAdapter(getActivity(), mFolders,
				lv_local_folder);
		lv_local_folder.setAdapter(mFolderAdapter);
		lv_local_folder.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

				MODE_TYPE = FOLDER_FILE_MODE;
				String path = mFolders.get(position).getMediaFolderPath();
				backButton.setText(path);
				getFolder(LocalActivity.MSG_GET_MEDIA_FOLDER_FILE, path);
				changeModeType(MODE_TYPE);
			}
		});
		getFolder(LocalActivity.MSG_GET_MEDIA_FOLDER, null);
	}

	// 改变显示模式
	public void changeModeType(int modeType) {
		if (modeType == FOLDER_MODE) { // 文件夹模式
			if (mFolderClickListener != null) {
				mFolderClickListener.showFolders();
			}
			MODE_TYPE = FOLDER_MODE;
			folder_file_view.setVisibility(View.GONE);
			lv_local_folder.setVisibility(View.VISIBLE);
		} else if (modeType == FOLDER_FILE_MODE) {
			if (mFolderClickListener != null) {
				mFolderClickListener.showMedias();
			}
			MODE_TYPE = FOLDER_FILE_MODE;
			L.i(TAG, "mFolderFiles:" + mFolderFiles.size());
			folder_file_view.setVisibility(View.VISIBLE);
			lv_local_folder.setVisibility(View.GONE);
		}
	}

	/**
	 * 
	 * 获取本地视频的文件夹 或文件夹中的视频
	 * 
	 * @param MediaType
	 *            用来区分获取文件夹还是文件夹中的视频
	 * @param path
	 *            获取文件夹中的视频时,传入文件夹的路径 其他传null
	 * @return void
	 * @throws
	 */
	private void getFolder(int MediaType, String path) {
		if (mHandler == null) {
			L.i(TAG, "mHandler is null!");
			return;
		}
		switch (MediaType) {
		case LocalActivity.MSG_GET_MEDIA_FOLDER:
			mHandler.sendEmptyMessage(LocalActivity.MSG_GET_MEDIA_FOLDER);
			break;
		case LocalActivity.MSG_GET_MEDIA_FOLDER_FILE:
			Message message = mHandler.obtainMessage();
			message.what = LocalActivity.MSG_GET_MEDIA_FOLDER_FILE;
			message.obj = path;
			mHandler.sendMessage(message);
			break;

		default:
			break;
		}
	}

	private class VideosItemClickListener implements OnItemClickListener {


		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			
			// 将改集合的所有url传入播放器
			String[] MediaPath = new String[mFolderFiles.size()];
			for (int i = 0; i < mFolderFiles.size(); i++) {
				MediaPath[i] = mFolderFiles.get(i).getMediaFilePath();
				L.i(TAG, "mediaPath:" + mFolderFiles.get(i).getMediaFilePath());
			}
			Intent intent = new Intent(getActivity(), FonePlayerActivity.class);
			intent.putExtra("from", PlayerFrom.LOCAL_INTERNAL);
			intent.putExtra("index", position);
			intent.putExtra("urls", MediaPath);
			getActivity().startActivity(intent);

		}

	}

	public void setFolderList(List<MediaFolder> Folders,
			List<MediaFile> MediaFiles) {
		this.mFolders = Folders;
		this.mFolderFiles = MediaFiles;
	}

	public void notifyData(int ModeType) {
		if (ModeType == FOLDER_MODE && mFolderAdapter != null) {
			mFolderAdapter.notifyDataSetChanged();
		} else if (ModeType == FOLDER_FILE_MODE && mFolderFileAdapter != null) {
			mFolderFileAdapter.notifyDataSetChanged();
		}
	}

	public void setIFolderClickListener(IFolderClickListener listener) {
		this.mFolderClickListener = listener;
	}

	public interface IFolderClickListener {
		void showMedias();

		void showFolders();
	}
}
